package com.lovestacks.tj.controller;

import com.lovestacks.tj.bean.NewMediaAllKindInfo;
import com.lovestacks.tj.bean.NewMediaOperation;
import com.lovestacks.tj.bean.ResultBean;
import com.lovestacks.tj.bean.ScoreAllKindInfo;
import com.lovestacks.tj.service.NewMediaOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("passInfo")
@Controller
public class PassInfoController {
    @Autowired
    private NewMediaOperationService newMediaOperationService;

    @RequestMapping("saveMedia")
    @ResponseBody
    public ResultBean saveMedia(String kind, String openId,@RequestParam(required = false,defaultValue = "0") int validate, String pname, String pphone, String work, String pdepart, String pmenu, String pic, String explain, int code, String theme, Date date, String type, String sname) {
        ResultBean bean = new ResultBean();
        try{
            newMediaOperationService.genNewMedia(kind,openId, pname, pphone, work, pdepart, pmenu, pic, explain, code, theme, date, type, sname,validate);
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "插入或修改成功");
            bean.makeSuccess(map);
        }catch (Exception e){
            bean.makeFail(e.getMessage());
        }
        return bean;
    }
    @RequestMapping("getMedia")
    @ResponseBody
    public ResultBean getMedia(String kind,@RequestParam(required = false,defaultValue = "0") int validate, String openId) {
        ResultBean bean = new ResultBean();
        try{
            Map<String, Object> info = newMediaOperationService.getNewMedia(kind, validate, openId);
            if (info == null){
                bean.makeEmpty();
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("data", info);
                bean.makeSuccess(map);
            }
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }


    @RequestMapping("saveAddScore")
    @ResponseBody
    public ResultBean saveAddScore(String kind, String openId,@RequestParam(required = false,defaultValue = "0") int validate, String fname,String zname,Date fdate,String author,String pic,String explain,int code,@RequestParam(required = false,defaultValue = "0") int rank,String mediaList) {
        ResultBean bean = new ResultBean();
        try{
            newMediaOperationService.genScore(kind, openId,validate, fname,zname,fdate,author,pic,explain,code,rank,mediaList);
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "插入或修改成功");
            bean.makeSuccess(map);
        }catch (Exception e){
            bean.makeFail(e.getMessage());
        }
        return bean;
    }
    @RequestMapping("getScore")
    @ResponseBody
    public ResultBean getScore(String kind,@RequestParam(required = false,defaultValue = "0") int validate, String openId) {
        ResultBean bean = new ResultBean();
        try{
            Map<String, Object> info = newMediaOperationService.getScore(kind, validate, openId);
            if (info == null){
                bean.makeEmpty();
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("data", info);
                bean.makeSuccess(map);
            }
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }



    @RequestMapping("getMediaList")
    @ResponseBody
    public ResultBean getMediaList(String kind,String openId) {
        ResultBean bean = new ResultBean();
        try{
            List<Map<String, Object>> infos = newMediaOperationService.getList(kind, openId);
            Map<String, Object> map = new HashMap<>();
            map.put("infos", infos);
            bean.makeSuccess(map);
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }
    @RequestMapping("getScoreList")
    @ResponseBody
    public ResultBean getScoreList(String kind,String openId) {
        ResultBean bean = new ResultBean();
        try{
            List<Map<String, Object>> infos = newMediaOperationService.getListScore(kind, openId);
            Map<String, Object> map = new HashMap<>();
            map.put("infos", infos);
            bean.makeSuccess(map);
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }



    @RequestMapping("scanList")
    @ResponseBody
    public ResultBean scanList(String openId) {
        ResultBean bean = new ResultBean();
        try{
            List<Map<String, Object>> infos = newMediaOperationService.scanList( openId);
            Map<String, Object> map = new HashMap<>();
            map.put("infos", infos);
            bean.makeSuccess(map);
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }

    @RequestMapping("showDetail")
    @ResponseBody
    public ResultBean showDetail(Integer id) {
        ResultBean bean = new ResultBean();
        try{
            Map<String, Object> data = newMediaOperationService.getMeidaShow(id);
            Map<String, Object> map = new HashMap<>();
            map.put("data", data);
            bean.makeSuccess(map);
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }




}
